package com.baidu.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;

import cn.hutool.core.util.StrUtil;
import com.baidu.entity.User;
import com.baidu.entity.vo.MyResult;
import com.baidu.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author Mawq
 * @Date 2021/8/27 10:53
 * @Version 1.0
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;

    @RequestMapping("/getAll")
    public MyResult getAllLists() {
        List<User> all = userMapper.getAll();
        System.out.println("查询出来的所有用户是：" + all);
        return MyResult.OK(all);
    }

    @GetMapping("/query/{id}")
    public MyResult getUserById(@PathVariable("id") Long id) {
        User user = userMapper.getUserById(id);
        System.out.println("通过ID查出来的用户：" + user);
        return MyResult.OK(user);
    }

    @GetMapping("/update/{id}")
    public MyResult updateUserById(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", "userName" + UUID.randomUUID().toString().replaceAll("-", ""));
        map.put("nickName", "nick" + UUID.randomUUID().toString().replaceAll("-", ""));
        map.put("id", id);
        int rest = userMapper.update(map);
        if (rest!=0){
            return MyResult.OK("随机数据更新成功",getAllLists());
        }
        return MyResult.OK("没有此用户，请你重新输入！");
    }

    @RequestMapping("/save")
    public MyResult saveUser() throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", RandomUtil.randomString("TomBarrtJune",3).toString().replace("-", ""));
        map.put("nickName", RandomUtil.randomInt(100,10000));
        map.put("sex", new Random().nextInt(10) % 2 == 1 ? "man" : "woman");
        System.out.println("map:" + map);
        int rest = userMapper.save(map);
        return MyResult.OK("随机添加成功",rest);
    }

    @RequestMapping("/update")
    public MyResult updateUser() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("username", "userName" + UUID.randomUUID().toString().replaceAll("-", ""));
        map.put("nickName", "nick" + UUID.randomUUID().toString().replaceAll("-", ""));
        Integer id1 = new Random().nextInt(10);
        User userById = userMapper.getUserById(id1);
        if (ObjectUtil.isNotNull(userById)) {
            map.put("id", id1);
            System.out.println(id1);
            System.out.println("map=" + map);
            int rest = userMapper.update(map);
            return MyResult.OK("随机数据更新成功");
        }
        return MyResult.error("此用户不存在！");
    }


    @RequestMapping("/del/{id}")
    public MyResult deleteUser(@PathVariable("id") Long id) throws Exception{
        int delete = userMapper.delete(id);
        return MyResult.OK("用户删除成功",delete);
    }

}
